var t = getApp()
Page({
  data: {
    list:[],
    navHeight:t.globalData.navHeight,
    title:'我的车辆',
  },
  onLoad: function (options) {

  },
  onShow(e){
    this.getList()
  },
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    this.getList()
  },
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
    setTimeout(function () {
      // 不加这个方法真机下拉会一直处于刷新状态，无法复位
      wx.stopPullDownRefresh()
    }, 1000)
  },
  add_car(e){
    wx.navigateTo({
      url: '/pages/personCenter/myCar/addCar/addCar',
    })
  },
  getList(e){
    var that = this
    t.request('/cars/list',{},function(res){
      console.log(res);
      if (res.code==200) {
          if (res.result.total>0) {
              t.globalData.carList = res.result
              wx.setStorageSync('carList', res.result);
              that.setData({
                list:res.result.list
              })
          }else{
            that.setData({
              list:res.result.list
            })
            wx.setStorageSync('carList',"");
            t.globalData.show_login = true
          }
      }
  })
  },
  delete_car(e){
    var that = this
    wx.showModal({
      title:'温馨提示',
      content:'确定删除该车辆吗',
      success(res){
        if (res.confirm) {
          t.request('/cars/delete',{car_id:e.currentTarget.dataset.car_id},function(e){
            console.log(e);
            if (e.code==200) {
                wx.showToast({
                  title: '删除成功',
                })
              that.getList()
            }else if(e.code==400){
              wx.showModal({
                title: e.msg,
                showCancel:false,
                complete:function(){
                }
              })
              /*wx.showToast({
                title: '请至少保留一条车辆信息！',
              })*/
            }
          })
        }else{

        }
      }
  })

  }
})